---------------------------------------------------------- 
--	Name: 		Alt Info Script	            			--
-- 	Version: 	3.0.0 r1								--					
--  Copyright 2011 S2 Games								--
----------------------------------------------------------

local _G = getfenv(0)
local AltInfo = _G[AltInfo] or {}
local ipairs, pairs, select, string, table, next, type, unpack, tinsert, tconcat, tremove, fmt, tostring, tonumber, tsort, ToPercent = _G.ipairs, _G.pairs, _G.select, _G.string, _G.table, _G.next, _G.type, _G.unpack, _G.table.insert, _G.table.concat, _G.table.remove, _G.string.format, _G.tostring, _G.tonumber, _G.table.sort, ToPercent		
local interface, interfaceName = object, object:GetName()
AltInfo.nameCompare, AltInfo.levelCompare, AltInfo.isHovering, AltInfo.showInventory, AltInfo.hasWard, AltInfo.hasHealth = '', 0, false, false, false, false
AltInfo.BAR_COLORING = GetCvarInt('ui_altInfoStyle', true) or 1 -- 1: Standard Gradient, 2: Solid Green/Red 3: Solid Yellow/Blue
AltInfo.HOVER_GLOW 	 = GetCvarBool('ui_altInfoGlow', true)
if (AltInfo.HOVER_GLOW == nil) then AltInfo.HOVER_GLOW = true end

local floor, min = _G.math.floor, _G.math.min
AltInfo.HealthPercent = 1.0
AltInfo.HasMana = false
AltInfo.IsAlly = false

local function SaveWidgetsToTable(tableName, destInterface)
	tableName.background_frame 		= interface:GetWidget('background_frame')
	tableName.border_frame 			= interface:GetWidget('border_frame')
	tableName.name_label 			= interface:GetWidget('name_label')
	tableName.main_panel 			= interface:GetWidget('main_panel')
	tableName.level_panel 			= interface:GetWidget('level_panel')
	tableName.level_bar 			= interface:GetWidget('level_bar')
	tableName.exp 					= interface:GetWidget('exp')
	tableName.health_lerp 			= interface:GetWidget('health_lerp')
	tableName.health_bar 			= interface:GetWidget('health_bar')
	tableName.mana_panel 			= interface:GetWidget('mana_panel')
	tableName.mana_bar 				= interface:GetWidget('mana_bar')
	tableName.health_bar_glow 		= interface:GetWidget('health_bar_glow')
	tableName.creep_plate_parent 	= interface:GetWidget('creep_plate_parent')
	tableName.health_bar_parent 	= interface:GetWidget('health_bar_parent')
	tableName.inventory 			= interface:GetWidget('inventory_container')

	tableName.lhiwrapper            = interface:GetWidget('lhiwrapper')
	tableName.lhi                   = interface:GetWidget('lhi')
end

local function iif(confition, trueValue, falseValue) --inline if, instead of C's (cond ? tru:fls)  
    if confition then  
        return trueValue  
    else  
        return falseValue  
    end  
end

local function UpdateLhaIndicator(self)
	local width
	if (AltInfo.IsAlly) then -- Ally
		if (AltInfo.HasMana) then -- Range ally
			width = ToPercent(Saturate(min(LHA.EffectiveDamage / LHA.EffectiveCreepHealthRangeAlly, 0.5)))
			if (AltInfo.HealthPercent > 0.5) then
				AltInfo.lhi:SetColor(0.0, 0.0, 0.0, 0.8)
			else
				if (LHA.EffectiveDamage < (AltInfo.HealthPercent * LHA.EffectiveCreepHealthRangeAlly)) then
					AltInfo.lhi:SetColor(AltInfo.NormalColor)
				else
					AltInfo.lhi:SetColor(AltInfo.LastHitColor)
				end
			end
		else -- Melee ally
			width = ToPercent(Saturate(min(LHA.EffectiveDamage / LHA.EffectiveCreepHealthMeleeAlly, 0.5)))
			if (AltInfo.HealthPercent > 0.5) then
				AltInfo.lhi:SetColor(0.0, 0.0, 0.0, 0.8)
			else
				if (LHA.EffectiveDamage < (AltInfo.HealthPercent * LHA.EffectiveCreepHealthMeleeAlly)) then
					AltInfo.lhi:SetColor(AltInfo.NormalColor)
				else
					AltInfo.lhi:SetColor(AltInfo.LastHitColor)
				end
			end
		end
	else -- Enemy
		if (AltInfo.HasMana) then -- Range enemy
			width = ToPercent(Saturate(LHA.EffectiveDamageEnemy / LHA.EffectiveCreepHealthRangeEnemy))
			if (LHA.EffectiveDamageEnemy < (AltInfo.HealthPercent * LHA.EffectiveCreepHealthRangeEnemy)) then
				AltInfo.lhi:SetColor(AltInfo.NormalColor)
			else
				AltInfo.lhi:SetColor(AltInfo.LastHitColor)
			end
		else -- Melee enemy
			width = ToPercent(Saturate(LHA.EffectiveDamageEnemy / LHA.EffectiveCreepHealthMeleeEnemy))
			if (LHA.EffectiveDamageEnemy < (AltInfo.HealthPercent * LHA.EffectiveCreepHealthMeleeEnemy)) then
				AltInfo.lhi:SetColor(AltInfo.NormalColor)
			else
				AltInfo.lhi:SetColor(AltInfo.LastHitColor)
			end
		end
	end

	AltInfo.lhiwrapper:SetWidth(width)
end

local function UpdateBackgroundFrame()
	if (AltInfo.nameCompare and string.len(AltInfo.nameCompare) > 0 and (AltInfo.ShowCreepNames or (AltInfo.nameCompare ~= "Soldier" and AltInfo.nameCompare ~= "Imp" and AltInfo.nameCompare ~= "Archer" and AltInfo.nameCompare ~= "Warlock" and AltInfo.nameCompare ~= "Heavy Ballista" and AltInfo.nameCompare ~= "Catapult" and AltInfo.nameCompare ~= "Tree"))) then
		AltInfo.background_frame:SetVisible(true)

		local baseWidth = GetStringWidth('dyn_10', AltInfo.nameCompare)
		if (AltInfo.showInventory) then
			baseWidth = math.max(baseWidth, AltInfo.inventory:GetWidth())
		end
		local frameWidth = baseWidth + AltInfo.background_frame:GetXFromString('1.2h')

		AltInfo.background_frame:SetX(frameWidth * -0.5)
		AltInfo.background_frame:SetWidth(frameWidth)

		if (AltInfo.showInventory) then
			AltInfo.background_frame:SetY('-6.5h')
			AltInfo.background_frame:SetHeight("4.6h")
		else
			AltInfo.background_frame:SetY('-4.7h')
			AltInfo.background_frame:SetHeight("2.3h")
		end
	
		AltInfo.border_frame:SetWidth('100%')
		AltInfo.name_label:SetWidth('100%')
		AltInfo.name_label:SetText(AltInfo.nameCompare)
	else
		AltInfo.background_frame:SetVisible(false)
	end
end

local function AltInfo2Team(self, team, isAlly)
	local team, isAlly = AtoN(team), AtoB(isAlly)

	if (team == 1 or team == 2) then
		if (AltInfo.EnableLastHitIndicator) then
			AltInfo.lhiwrapper:SetVisible(true)
			AltInfo.IsAlly = isAlly
			UpdateLhaIndicator(self)
		end
	else
		AltInfo.lhiwrapper:SetVisible(false)
	end
end

local function AltInfo2Name(self, name)
	if (name and (string.len(name) > 0)) then
		AltInfo.health_bar_glow:SetVisible(AltInfo.HOVER_GLOW)
		AltInfo.nameCompare = name
	else
		AltInfo.health_bar_glow:SetVisible(false)
		AltInfo.nameCompare = ''
	end
	UpdateBackgroundFrame()
end

local function AltInfo2HasMana(self, hasMana)
	if (AtoB(hasMana)) then
		AltInfo.mana_panel:SetVisible(true)
		AltInfo.creep_plate_parent:SetHeight('0.8h')
		--AltInfo.mana_panel:SetHeight('0.2h')
		AltInfo.health_bar_parent:SetHeight( AltInfo.creep_plate_parent:GetHeight() - AltInfo.mana_panel:GetHeight() )
		AltInfo.HasMana = true
	else
		AltInfo.mana_panel:SetVisible(false)
		AltInfo.creep_plate_parent:SetHeight('0.6h')
		AltInfo.health_bar_parent:SetHeight( AltInfo.creep_plate_parent:GetHeight() )
		AltInfo.HasMana = false
	end
	UpdateLhaIndicator(self)
end

local function AltInfo2Color(self, color, nameColor)
	local glowGreen, glowRed, barGreen, barRed, capGreen, capRed, lerpGreen, lerpRed, bgGreen, bgRed

	if (AltInfo.BAR_COLORING == 3) then
		glowGreen, glowRed = 	'1 1 0 1', 			'0 0.3 1 1'
		barGreen, barRed = 		'.88 .88 0 1', 		'0 0.25 1 1'
		lerpGreen, lerpRed = 	'0 0.3 1 0.9', 		'1 1 0 0.9'
		bgGreen, bgRed = glowGreen, glowRed
	else
		glowGreen, glowRed = 	'0 1 0 1', 			'1 0 0 1'
		barGreen, barRed = 		'0 .78 0 1', 		'0.8 0.15 0.15 1'
		capGreen, capRed = 		'0 0.88 0 1', 		'0.8 0.35 0.15 1'
		lerpGreen, lerpRed = 	'0.7 0 0 1', 		'1.0 1.0 0 0.7'	
		bgGreen, bgRed = glowGreen, glowRed
	end

	if color == '0.0000 1.0000 0.0000 1.0000' or color == '1.0000 1.0000 0.0000 1.0000'  then -- Self or Ally
		AltInfo.health_bar_glow:SetBorderColor(glowGreen)	
		AltInfo.health_lerp:SetColor(lerpGreen)
		AltInfo.HealthBarColor = {}
		if (AltInfo.BAR_COLORING == 3) then
			AltInfo.HealthBarColor.R = .88
			AltInfo.HealthBarColor.G = .88
			AltInfo.HealthBarColor.B = 0
		else
			AltInfo.HealthBarColor.R = 0
			AltInfo.HealthBarColor.G = 0.78
			AltInfo.HealthBarColor.B = 0
		end
		if (AltInfo.EnableTransparency) then
			AltInfo.health_bar:SetColor(AltInfo.HealthBarColor.R, AltInfo.HealthBarColor.G, AltInfo.HealthBarColor.B, Saturate(1.2 - (AltInfo.HealthPercent * 1.1)))
			AltInfo.creep_plate_parent:SetColor(0, 0, 0, Saturate(0.8 - 0.8 * AltInfo.HealthPercent))
		else
			AltInfo.health_bar:SetColor(barGreen)
			AltInfo.creep_plate_parent:SetColor(0, 0, 0, 1)
		end
		AltInfo.border_frame:SetBorderColor(bgGreen)
	elseif color == '1.0000 0.0000 0.0000 1.0000' then
		AltInfo.health_bar_glow:SetBorderColor(glowRed)	
		AltInfo.health_lerp:SetColor(lerpRed)
		AltInfo.HealthBarColor = {}
		if (AltInfo.BAR_COLORING == 3) then
			AltInfo.HealthBarColor.R = 0
			AltInfo.HealthBarColor.G = 0.25
			AltInfo.HealthBarColor.B = 1
		else
			AltInfo.HealthBarColor.R = 0.8
			AltInfo.HealthBarColor.G = 0.15
			AltInfo.HealthBarColor.B = 0.15
		end
		if (AltInfo.EnableTransparency) then
			AltInfo.health_bar:SetColor(AltInfo.HealthBarColor.R, AltInfo.HealthBarColor.G, AltInfo.HealthBarColor.B, Saturate(1.2 - (AltInfo.HealthPercent * 1.1)))
			AltInfo.creep_plate_parent:SetColor(0, 0, 0, Saturate(0.8 - 0.8 * AltInfo.HealthPercent))
		else
			AltInfo.health_bar:SetColor(barRed)
			AltInfo.creep_plate_parent:SetColor(0, 0, 0, 1)
		end
		AltInfo.border_frame:SetBorderColor(bgRed)	
	else
		AltInfo.health_bar:SetColor(color)
		AltInfo.health_lerp:SetColor(color)
		AltInfo.border_frame:SetBorderColor(color)
		AltInfo.health_bar_glow:SetBorderColor(color)	
		AltInfo.HealthBarColor = nil
		if (AltInfo.EnableTransparency) then
			AltInfo.creep_plate_parent:SetColor(0, 0, 0, Saturate(0.8 - 0.8 * AltInfo.HealthPercent))
		else
			AltInfo.creep_plate_parent:SetColor(0, 0, 0, 1)
		end
	end

end

local function AltInfo2ColorOld(self, color, nameColor)
	local glowColorString = '0 1 0 1'
	local lerpGreen = 	'0.7 0 0 1'
	local capGreen = 	'0 0.78 0 1'
	
	if (color == '0.0000 1.0000 0.0000 1.0000') or (color == '1.0000 1.0000 0.0000 1.0000') then -- self or ally
		glowColorString = '0 1 0 1'
	elseif (color == '1.0000 0.0000 0.0000 1.0000') then
		glowColorString = '1 0 0 1'
	end	
	
	AltInfo.health_lerp:SetColor(lerpGreen)
	AltInfo.border_frame:SetBorderColor(color)
	AltInfo.health_bar_glow:SetBorderColor(glowColorString)

	if (AltInfo.EnableTransparency) then
		AltInfo.creep_plate_parent:SetColor(0, 0, 0, Saturate(0.8 - 0.8 * AltInfo.HealthPercent))
	else
		AltInfo.creep_plate_parent:SetColor(0, 0, 0, 1)
	end
end

local function AltInfo2HasHealth(self, hasHealth)
	AltInfo.hasHealth = AtoB(hasHealth)
	
	AltInfo.main_panel:SetVisible(AltInfo.hasHealth or AltInfo.levelCompare > 0)
end

local function AltInfo2HealthPercent(self, healthPercent)
	local healthPercent = AtoN(healthPercent)
	AltInfo.health_bar:SetWidth(ToPercent(healthPercent))

	if (AltInfo.EnableTransparency and AltInfo.HealthBarColor ~= nil) then
		AltInfo.health_bar:SetColor(AltInfo.HealthBarColor.R, AltInfo.HealthBarColor.G, AltInfo.HealthBarColor.B, Saturate(1.2 - (healthPercent * 1.1)))
		AltInfo.creep_plate_parent:SetColor(0, 0, 0, Saturate(0.8 - 0.8 * healthPercent))
	end

    AltInfo.HealthPercent = healthPercent
	UpdateLhaIndicator(self)
end

local function AltInfo2HealthPercentOld(self, healthPercent)
	local healthPercent = AtoN(healthPercent)
	AltInfo.health_bar:SetWidth(ToPercent(healthPercent))

	if (AltInfo.EnableTransparency) then
		AltInfo.health_bar:SetColor(Saturate(1 - (healthPercent - 0.50) / 0.50), (healthPercent + (((healthPercent - 0.05) / 1.0) * 0.2)) / 0.45, 0, Saturate(1.2 - (healthPercent * 1.1)))
		AltInfo.creep_plate_parent:SetColor(0, 0, 0, Saturate(0.8 - 0.8 * healthPercent))
	else
		AltInfo.health_bar:SetColor(Saturate(1 - (healthPercent - 0.50) / 0.50), (healthPercent + (((healthPercent - 0.05) / 1.0) * 0.2)) / 0.45, 0, 1)
	end	

    AltInfo.HealthPercent = healthPercent
	UpdateLhaIndicator(self)
end

local function AltInfo2ManaPercent(self, manaPercent)
	AltInfo.mana_bar:SetWidth(ToPercent(manaPercent))
end

local function AltInfo2HealthLerp(self, healthLerp)
	AltInfo.health_lerp:SetWidth(ToPercent(healthLerp))
end

local function AltInfo2IsInExpRange(self, isInExpRange)
	AltInfo.exp:SetVisible(AtoB(isInExpRange))
end

local function AltInfo2ShowInventory(self, showInventory)
	AltInfo.showInventory = AtoB(showInventory)
	AltInfo.inventory:SetVisible(AltInfo.showInventory)
	UpdateBackgroundFrame()
end

local function AltInfo2Level(self, level)
	local level = tonumber(level) or 0
	AltInfo.levelCompare = level	
	
	AltInfo.main_panel:SetVisible(AltInfo.hasHealth or AltInfo.levelCompare > 0)
	
	if (level > 0) then
		AltInfo.level_panel:SetVisible(true)
		AltInfo.level_bar:SetWidth(ToPercent(level)..'%')
	else
		AltInfo.level_panel:SetVisible(false)
	end	
	UpdateBackgroundFrame()
end

function AltInfo2HasWard(hasWard)
	if (hasWard ~= AltInfo.hasWard) then
		UpdateBackgroundFrame()
		AltInfo.hasWard = hasWard
	end
end

local function AltInfoRevisedShowCreepNameChanged()
	AltInfo.ShowCreepNames = GetCvarBool('AltInfoRevisedShowCreepName', true)
	if (AltInfo.ShowCreepNames == nil) then AltInfo.ShowCreepNames = false end
end

local function AltInfoRevisedEnableTransparencyChanged()
	AltInfo.EnableTransparency = GetCvarBool('AltInfoRevisedEnableTransparency', true)
	if (AltInfo.EnableTransparency == nil) then AltInfo.EnableTransparency = true end
end

local function AltInfoRevisedEnableLastHitIndicatorChanged()
	AltInfo.EnableLastHitIndicator = GetCvarBool('AltInfoRevisedEnableLastHitIndicator', true)
	if (AltInfo.EnableLastHitIndicator == nil) then AltInfo.EnableLastHitIndicator = true end
end

local function AltInfoRevisedLastHitIndicatorColorIndexChanged()
	local lhiColorIndex = GetCvarInt('AltInfoRevisedLastHitIndicatorColorIndex', true)

	AltInfo.NormalColor  = '1.0000 1.0000 1.0000 1.0000'
	AltInfo.LastHitColor = '0.0000 0.0000 0.0000 0.0000'

	if (lhiColorIndex == 1) then
		AltInfo.LastHitColor = '0.0000 1.0000 0.0000 1.0000'
		return
	end
	if (lhiColorIndex == 2) then
		AltInfo.LastHitColor = '1.0000 0.0000 0.0000 1.0000'
		return
	end
	if (lhiColorIndex == 3) then
		AltInfo.LastHitColor = '0.0000 0.0000 1.0000 1.0000'
		return
	end
	if (lhiColorIndex == 4) then
		AltInfo.NormalColor  = '1.0000 1.0000 1.0000 0.7000'
		AltInfo.LastHitColor = '1.0000 1.0000 1.0000 1.0000'
		return
	end
end

local function Initialize()	
	
	AltInfo.BAR_COLORING = GetCvarInt('ui_altInfoStyle', true) or 1 -- 1: Standard Gradient, 2: Solid Green/Red 3: Solid Yellow/Blue
	AltInfo.HOVER_GLOW 	 = GetCvarBool('ui_altInfoGlow', true)
	if (AltInfo.HOVER_GLOW == nil) then AltInfo.HOVER_GLOW = true end
	
	SaveWidgetsToTable(AltInfo, 'alt_info')
	
	if (AltInfo.BAR_COLORING == 2) or (AltInfo.BAR_COLORING == 3) then
		interface:RegisterWatch('AltInfo2Color', AltInfo2Color )
		interface:RegisterWatch('AltInfo2HealthPercent', AltInfo2HealthPercent )
	else
		interface:RegisterWatch('AltInfo2Color', AltInfo2ColorOld )
		interface:RegisterWatch('AltInfo2HealthPercent', AltInfo2HealthPercentOld )
	end	
	
	interface:RegisterWatch('AltInfo2Name', AltInfo2Name )
	interface:RegisterWatch('AltInfo2Team', AltInfo2Team )
	interface:RegisterWatch('AltInfo2HasMana', AltInfo2HasMana )
	interface:RegisterWatch('AltInfo2HasHealth', AltInfo2HasHealth )
	interface:RegisterWatch('AltInfo2ManaPercent', AltInfo2ManaPercent )
	interface:RegisterWatch('AltInfo2HealthLerp', AltInfo2HealthLerp )
	interface:RegisterWatch('AltInfo2IsInExpRange', AltInfo2IsInExpRange )
	interface:RegisterWatch('AltInfo2ShowInventory', AltInfo2ShowInventory)
	interface:RegisterWatch('AltInfo2Level', AltInfo2Level )
	
    UIManager.GetInterface('main'):RegisterWatch('AltInfoOptionsUpdated2', Initialize)

    interface:RegisterWatch('LHAUpdated', UpdateLhaIndicator)
	UIManager.GetInterface('main'):RegisterWatch('AltInfoRevisedOptionsChanged', Initialize)
	UIManager.GetInterface('main'):RegisterWatch('AltInfoRevisedShowCreepNameChanged', AltInfoRevisedShowCreepNameChanged)
	UIManager.GetInterface('main'):RegisterWatch('AltInfoRevisedEnableTransparencyChanged', AltInfoRevisedEnableTransparencyChanged)
	UIManager.GetInterface('main'):RegisterWatch('AltInfoRevisedEnableLastHitIndicatorChanged', AltInfoRevisedEnableLastHitIndicatorChanged)
	UIManager.GetInterface('main'):RegisterWatch('AltInfoRevisedLastHitIndicatorColorIndexChanged', AltInfoRevisedLastHitIndicatorColorIndexChanged)

	-- Option initializations
	AltInfoRevisedShowCreepNameChanged()
	AltInfoRevisedEnableTransparencyChanged()
	AltInfoRevisedEnableLastHitIndicatorChanged()
	AltInfoRevisedLastHitIndicatorColorIndexChanged()
end

Initialize()		

	






